﻿//  Write a program that finds in given array of integers a sequence of given sum S (if present). 
//  Example:	 {4, 3, 1, 4, 2, 5, 8}, S=11  {4, 2, 5}	


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _10FindSequenceGivenSum
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Size of array:");
            string strArraySize = Console.ReadLine();
            int arraySize = int.Parse(strArraySize);
            Console.Write("The sum S:");
            string strSum = Console.ReadLine();
            int userSum = int.Parse(strSum);
            int[] array = new int[arraySize];
            for (int i = 0; i <= array.Length - 1; i++)
            {
                Console.Write("array[{0}]:", i);
                string strValue = Console.ReadLine();
                array[i] = int.Parse(strValue);
            }
            Console.WriteLine("The array looks like this:");
            foreach (var item in array)
            {
                Console.Write(item + " ");
            }
            int currentSum = 0;
            int elementIndex = 0;
            int elementIndexEnd = 0;
            int total = 0;
            for (int i = 0; i <= array.Length - 1; i++)
            {
                currentSum = 0;
                for (int j = i; j <= array.Length - 1; j++)
                {
                    currentSum = currentSum + array[j];
                    if (userSum == currentSum)
                    {
                        elementIndex = i;
                        elementIndexEnd = j;
                        break;
                    }
                    else
                    {
                        continue;
                    }
                }
            }
            Console.WriteLine();
            Console.WriteLine("The sequence which gives the required sum is:");
            for (int i = elementIndex; i <= elementIndexEnd; i++)
            {
                Console.Write(array[i] + " ");
            }
            Console.WriteLine();
        }
    }
}
